package com.edu.zju.scs.panacea.mapper.plan;

import com.edu.zju.scs.panacea.entity.plan.Material;
import com.edu.zju.scs.panacea.entity.plan.Plan;
import com.edu.zju.scs.panacea.entity.query.MaterialQuery;
import com.edu.zju.scs.panacea.entity.vo.MaterialCheckVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-01-11 16:19
 */
@Repository
public interface MaterialMapper {

    @Select("SELECT * FROM plan_material WHERE id = #{id}")
    @Results(id="materialMap", value={
            @Result(id=true,property="id",column="id"),
            @Result(property="taskId",column="task_id"),
            @Result(property="templateId",column="template_id"),
            @Result(property="url",column="url"),
            @Result(property="state",column="state"),
            @Result(property="type",column="type"),
            @Result(property="seconds",column="seconds"),
            @Result(property="remark",column="remark")})
    Material selectMaterial(Long id);


    @Select("SELECT * FROM plan_material")
    @ResultMap(value = "materialMap")
    List<Material> selectAllMaterial();

    @Delete("DELETE FROM plan_material where id = #{id}")
    long deleteMaterial(Long id);

    @Insert("INSERT INTO plan_material(task_id, template_id, url, state, type, seconds, remark) VALUES (#{taskId}, #{templateId}, #{url}, 0, #{type}, #{seconds}, #{remark})")
    @Options(useGeneratedKeys=true)
    long insertMaterial(Material material);

    @Update({ "update plan_material set task_id = #{taskId}, template_id = #{templateId}, url = #{url}, state = #{state}, remark = #{remark}, type = #{type}, seconds = #{seconds} where id = #{id}" })
    long updateMaterial(Material material);


    @Select("<script> " +
            "SELECT M.id, P.id as plan_id, P.type, L.id as loc_id, L.name as loc_name, D.id as site_id, S.name as site_name, M.url, M.state, T.start_date, T.end_date, T.bin_launch_time from plan_material as M join res_loc as L join plan_task as T join plan as P join res_dev as D join res_site as S " +
            "where M.task_id = T.id and  T.plan_id = P.id and T.loc_id = L.id and L.dev_id = D.id and D.site_id = #{siteId} and D.site_id = S.id " +
            " </script>")
    @Results(value={
            @Result(property="id",column="id"),
            @Result(property="planId",column="plan_id"),
            @Result(property="type",column="type"),
            @Result(property="locId",column="loc_id"),
            @Result(property="locName",column="loc_name"),
            @Result(property="siteId",column="site_id"),
            @Result(property="siteName",column="site_name"),
            @Result(property="url",column="url"),
            @Result(property="state",column="state"),
            @Result(property="startDate",column="start_date"),
            @Result(property="endDate",column="end_date"),
            @Result(property="binLaunchTime",column="bin_launch_time")})
    List<MaterialCheckVO> selectMaterialToCheck(Long siteId);


    @Delete("DELETE FROM plan_material where task_id = #{taskId}")
    long deleteMaterialByTask(Long taskId);

    @Update({ "update plan_material set state = 1 where id = #{id}" })
    long passMaterial(Long id);

    @Update({ "update plan_material set state = 2, remark = #{remark} where id = #{id}" })
    long rejectMaterial(Long id, String remark);


    @ResultMap(value = "materialMap")
    @Select("<script> " +
            "SELECT * from plan_material where 1=1 "+
            " <if test='materialQuery.planId != null'> and task_id in (SELECT id from plan_task where plan_id = #{materialQuery.planId}) </if> "+
            " <if test='materialQuery.locId != null'> and task_id in (SELECT id from plan_task where loc_id = #{materialQuery.locId}) </if> "+
            " </script>")
    List<Material> selectSpecificMaterials(@Param("materialQuery") MaterialQuery materialQuery);

}

